package com.aistar.controller;

import com.aistar.pojo.Cart;
import com.aistar.pojo.CartDetails;
import com.aistar.pojo.Customer;
import com.aistar.pojo.Product;
import com.aistar.service.CartDetailsService;
import com.aistar.service.CartService;
import com.aistar.service.ProductService;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @Autowired
    private CartDetailsService cartDetailsService;

    @Autowired
    private ProductService productService;

//注册时候生成一个购物车
    @PostMapping("/add")
    @ResponseBody
    public ServerResponse addCart(Integer custId){
        Cart cart = new Cart();
        cart.setCustomerId(custId);
        cart.setCartTime(new Date());
        cart.setCartSize(50);
        cart.setCartStatus(1);
        ServerResponse serverResponse = cartService.save(cart);
        return serverResponse;
    }

    @GetMapping("/add/{customerId}/{productId}")
    @ResponseBody
    /*用户点击商品按钮将其加入购物车 判断购物车中的商品数量*/
    public ServerResponse addProduct(@PathVariable("customerId")Integer customerId,@PathVariable("productId")Integer productId,HttpServletRequest request){
        //通过用户id获取到购物车id

        ServerResponse se = cartService.getCartIdByCustomerId(customerId);
        Integer cartId = (Integer) se.getData();
        ServerResponse se2 = cartDetailsService.getOneByCartId(cartId,productId);
        if (se2.getStatus()==0){
            //判断当前用户购物车商品的数量
            System.out.println("此时在判断用户购物车商品的数量");
            ServerResponse serverResponse = cartService.getCartProductCount(customerId);
            Integer count = (Integer) serverResponse.getData();
            System.out.println("购物车商品的数量count："+count);
            CartDetails cartDetails= null;
            if (count<20){
                cartDetails = new CartDetails();
                cartDetails.setCartId(cartId); //购物车id
                cartDetails.setProductId(productId); //商品id
                cartDetails.setCartDetailsStatus(1);//1是有效的状态
                cartDetails.setCartDetailsTime(new Date());
                cartDetails.setCartDetailsSize(1);//刚点击添加商品的时候默认是为1的
            }


            //插入到数据库表中
            return cartDetailsService.save(cartDetails);
        }else {
            return ServerResponse.getDataFailed("您的购物车中以添加过该商品");
        }


    }



    //查看购物车（分页）
    @GetMapping("/getAll/{customerId}")
    @ResponseBody
    public ServerResponse getAllByPage(@PathVariable("customerId")Integer customerId){

        //service层查询所有购物车所有的商品列表 (分页)
        ServerResponse serverResponse = cartService.getAllByCustomerId(customerId);

        return serverResponse;
    }
    //购物车中商品的个数
    @GetMapping("/getCount/{customerId}")
    @ResponseBody
    public ServerResponse getCount(@PathVariable("customerId")Integer customerId){
        ServerResponse serverResponse = cartService.getCartProductCount(customerId);
        return serverResponse;
    }


    //删除购物车中的某一个商品
    @GetMapping("/delete/{customerId}/{productId}")
    @ResponseBody
    public ServerResponse deleteProduct(@PathVariable("customerId")Integer customerId,@PathVariable("productId")Integer productId){
        //根据用户id获取到购物车id
        ServerResponse se = cartService.getCartIdByCustomerId(customerId);
        Integer cartId = (Integer) se.getData();
        //通过购物车id productId 来获取购物车详情表的记录
        ServerResponse serverResponse = cartDetailsService.getOneByCartId(cartId,productId);
        CartDetails cartDetails = null;
        if (serverResponse.getStatus()==1){
            List<CartDetails> cartDetailsList = (List<CartDetails>) serverResponse.getData();
             cartDetails = cartDetailsList.get(0);

        }
        return  cartDetailsService.modifyStatus(cartDetails);
    }


    //清空购物车
    @GetMapping("/clear/{customerId}")
    @ResponseBody
    public ServerResponse clearCart(@PathVariable("customerId")Integer customerId){
        //拿到用户id获取购物车id
        ServerResponse se = cartService.getCartIdByCustomerId(customerId);
        Integer cartId = (Integer) se.getData();
        //通过购物车id 来获得所有此购物车id的详情jilu
        ServerResponse serverResponse = cartDetailsService.getAllByCartId(cartId);
        if (serverResponse.getStatus()==1){
            List<CartDetails> cartDetailsList = (List<CartDetails>)serverResponse.getData();

            for (CartDetails cartDetails :cartDetailsList){
                  cartDetailsService.modifyStatus(cartDetails);
            }

        }
        //再次查询
        ServerResponse sr = cartDetailsService.getAllByCartId(cartId);
        if (serverResponse.getStatus()==1){
            return ServerResponse.getDataFailed("未清空");
        }else {
            return ServerResponse.getDataSuccess("购物车清空成功");
        }

    }



    @PostMapping("/product/addcart")
    @ResponseBody
    public ServerResponse addcart(Integer productId,HttpServletRequest request){
        Customer customer = (Customer)request.getSession().getAttribute("loginCustomer");
        Integer customerId = customer.getCustomerId();
        ServerResponse serverResponse = cartService.getCartIdByCustomerId(customerId);
        System.out.println("serverResponse:"+serverResponse);
        if (serverResponse.getStatus()==1){
            Integer cartId = (Integer)serverResponse.getData();
            System.out.println("cartId"+cartId);
            CartDetails cartDetails = new CartDetails();
            cartDetails.setCartId(cartId);
            cartDetails.setProductId(productId);
            cartDetails.setCartDetailsSize(1);
            cartDetails.setCartDetailsTime(new Date());
            cartDetails.setCartDetailsStatus(1);
            ServerResponse serverResponse1 = cartDetailsService.save(cartDetails);
            if (serverResponse1.getStatus()==1){
                return serverResponse1;
            }
            return serverResponse1;
        }
        return serverResponse;
    }





}

